//
//  Strategy.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/1.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "Strategy.h"
#import "StrategyCollectionView.h"
#import "StrateBigCollectionViewCell.h"
#import "StrategyCollectionViewCell.h"
#import "BigStrategyCollectionReusableView.h"
#import "StrategyCollectionReusableView.h"
#import "StrateModel.h"
#import "NetworkHandler.h"
#import "ToolDB.h"
#import "TAMacro.h"
#import "JXLDayAndNightMode.h"
@interface Strategy ()<BigStrategyCollectionViewDelegate>


@end


@implementation Strategy

- (NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}

- (NSMutableArray *)array2{
    if (!_array2) {
        self.array2 = [NSMutableArray array];
    }
    return _array2;
    
    
}

- (instancetype)initWithFrame:(CGRect)frame{
    self  = [super initWithFrame:frame];
    if (self) {
        [self creatSubview:frame];
        [self analysis];
    }
    return self;
}



- (void)analysis{
    
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/channel_groups/all" BodyString:nil WithDataBlock:^(id result) {
        
        
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"channel_groups"];
        
        for (NSDictionary *dic1 in arr) {
            NSArray *arr = [dic1 objectForKey:@"channels"];
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dic2 in arr) {
                StrateModel *model = [StrateModel modelWithDic:dic2];
                
                [tempArray addObject:model];
                
            }
            [self.array addObject:tempArray];
        }
        [self.collectionViewBig reloadData];
        
    }];
}


- (void)creatSubview:(CGRect)frame{
    //self.backgroundColor = [UIColor greenColor];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH/ 5 ,SCREEN_WIDTH / 5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); // 内边距
    flowLayout.headerReferenceSize = CGSizeMake(0, 100);
    self.collectionViewBig = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -SCREEN_HEIGHT / 7 ) collectionViewLayout:flowLayout];
    
    
    
    self.collectionViewBig.delegate = self;
    self.collectionViewBig.dataSource = self;
    self.collectionViewBig.pagingEnabled = YES;
    
    
    [self.collectionViewBig registerClass:[BigStrategyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BigheadView"];
    
    
    [self.collectionViewBig registerClass:[StrategyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView"];
    
    
    
    [self.collectionViewBig registerClass:[StrateBigCollectionViewCell class] forCellWithReuseIdentifier:@"StrateBigCell"];
    [self.collectionViewBig jxl_setDayMode:^(UIView *view) {
        self.collectionViewBig.backgroundColor = [UIColor whiteColor];
        
        
    } nightMode:^(UIView *view) {
        self.collectionViewBig.backgroundColor = [UIColor grayColor];
        
    }
     ];
    
    [self addSubview:_collectionViewBig];
    
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0 ) {
        return CGSizeMake(0, SCREEN_HEIGHT /4.5 );
        
    }else{
        return CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT/ 20);
        
    }
    
}

// 对比cell的使用 注册完头视图和尾视图之后需要实现下面这个方法
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    // 如果是头视图
    if ([kind  isEqualToString:UICollectionElementKindSectionHeader] && indexPath.section == 0) {
        
        self.big= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BigheadView" forIndexPath:indexPath];
        
        self.big.delegate = self;
        
        [self.big jxl_setDayMode:^(UIView *view) {
            self.big.backgroundColor = [UIColor whiteColor];
        } nightMode:^(UIView *view) {
            self.big.backgroundColor = [UIColor grayColor];
            
            
        }
         ];
        
        return self.big;
    }else{
        
        StrategyCollectionReusableView *headView= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headView" forIndexPath:indexPath];
        
        
        [headView jxl_setDayMode:^(UIView *view) {
            
            headView.backgroundColor = [UIColor whiteColor];
        } nightMode:^(UIView *view) {
            headView.backgroundColor = [UIColor grayColor];
            
            
        }];
        
        
        if (indexPath.section == 1) {
            headView.Title.text =@"风格";
            
        }else if (indexPath.section == 2){
            headView.Title.text = @"品类";
        }
        
        
        return headView;
    }
    
    
}


// item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return [[self.array objectAtIndex:section] count];
    
}


// 分区数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    return self.array.count;
}




- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    StrateBigCollectionViewCell *StrateBigCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StrateBigCell" forIndexPath:indexPath];
    
    StrateModel *model = [[self.array objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
    StrateBigCell.strateModel = model;
    
    return StrateBigCell;
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    StrateModel *model = [[self.array objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
    
    self.str = [NSString stringWithFormat:@"%ld",(long)model.Id];
    
    
    [self.delegate didClickSelectItemAtIndexPathStrategy:indexPath str:self.str STR:model.title IMAGE:model.icon_url];
    
}
- (void)didClickSelectItemAtIndexPathBigStrategy:(NSIndexPath *)indexPath str:(NSString *)str{
    
    self.str2 = str;
    
    [self.deledages didClickSelectItemAtIndexPathStrategyBig:indexPath str:self.str2];
}

@end
